package com.zjdiepu.www.biz.dz;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.BusinessExtrasCodeType;
import com.zjdiepu.www.base.enums.type.BusinessExtrasType;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dz.DzGpsMapper;
import com.zjdiepu.www.dao.yw.YwBusinessExtrasMapper;
import com.zjdiepu.www.ibiz.dz.IDzGpsService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.dz.dto.DzGps;
import com.zjdiepu.www.model.dz.ext.DzGpsExt;
import com.zjdiepu.www.model.yw.dto.YwBusinessExtras;
import com.zjdiepu.www.model.yw.request.ChangeGpsRequest;

/**
 * Service层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年06月01日 19时14分<br/>
 * @since 1.0<br/>
 */
@Service
public class DzGpsServiceImpl extends BaseService implements IDzGpsService {
	public static final Logger logger = LoggerFactory.getLogger(DzGpsServiceImpl.class);

	@Autowired
	private DzGpsMapper dzGpsMapper;
	@Autowired
	private YwBusinessExtrasMapper ywBusinessExtrasMapper;
	
	@Override
	public void saveChangeGps(ChangeGpsRequest changeGpsRequest, SysUser sysUser) {
		String busiId = changeGpsRequest.getId();
    	
    	//判断是否已经保存
		YwBusinessExtras ywBusinessExtras_ = new YwBusinessExtras();
		ywBusinessExtras_.setBusiId(busiId);
		ywBusinessExtras_.setTypeExtras(BusinessExtrasType.AUDIT.getCode());
		ywBusinessExtras_.setCodeExtras(BusinessExtrasCodeType.ADD_GPS.getCode());
		ywBusinessExtras_.setDpStatus(DpStatus.NORMAL.getCode());
		YwBusinessExtras ywBusinessExtrasDb = ywBusinessExtrasMapper.selectRefined(ywBusinessExtras_);
		boolean newYwBusinessExtras = false;
		if(null == ywBusinessExtrasDb) {
			newYwBusinessExtras = true;
			ywBusinessExtrasDb = new YwBusinessExtras();
			ywBusinessExtrasDb.initInsertData(sysUser.getId(), sysUser.getName());
		} else {
			ywBusinessExtrasDb.initUpdateData(sysUser.getId(), sysUser.getName());
		}
		ywBusinessExtrasDb.setBusiId(busiId);
		ywBusinessExtrasDb.setTypeExtras(BusinessExtrasType.AUDIT.getCode());
		ywBusinessExtrasDb.setCodeExtras(BusinessExtrasCodeType.ADD_GPS.getCode());
		ywBusinessExtrasDb.setNameExtras(BusinessExtrasCodeType.ADD_GPS.getValue());
		ywBusinessExtrasDb.setValueExtras(changeGpsRequest.getValueExtras()); //0：表示不需要电审   1：需要电审
		if(newYwBusinessExtras) {
			ywBusinessExtrasMapper.insert(ywBusinessExtrasDb);
		} else {
			ywBusinessExtrasMapper.updateByPrimaryKeySelective(ywBusinessExtrasDb);
		}
	}
	
	@Override
	public void saveUpdateAndDelete(DzGps dzGps, OperateType operType) {
		switch (operType) {
		case add:
			dzGps.initInsertData(dzGps.getModifier(), dzGps.getModifierName());
			dzGpsMapper.insert(dzGps);
			break;
		case edit:
			dzGps.initUpdateData(dzGps.getModifier(), dzGps.getModifierName());
			// 影响的记录数
			int updateCount = dzGpsMapper.updateByPrimaryKeySelective(dzGps);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dzGps.initDeleteData(dzGps.getModifier(), dzGps.getModifierName());
			// 影响的记录数
			int deleteCount = dzGpsMapper.updateByPrimaryKeySelective(dzGps);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DzGps> selectByExt(DzGpsExt dzGpsExt) {
		return dzGpsMapper.selectByExt(dzGpsExt);
	}
	
	@Override
	public int countByExt(DzGpsExt dzGpsExt){
		return dzGpsMapper.countByExt(dzGpsExt);
	}
	
	@Override
	public DzGps selectById(String id) {
		return dzGpsMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dzGpsMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DzGps dzGps) {
		return dzGpsMapper.countRefined(dzGps);
	}

	@Override
	public DzGps selectRefined(DzGps dzGps) {
		return dzGpsMapper.selectRefined(dzGps);
	}

	@Override
	public boolean repeat(DzGps dzGps) {
		int count = countRefined(dzGps);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dzGps.getId())) {
			return true;
		}
		DzGps dbItem = selectRefined(dzGps);
		if(null == dbItem) {
			return false;
		}
		return !dzGps.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dzGpsMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
}
